﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GeneticAlgorythm.Algorythm;

namespace GeneticAlgorythm.Problem
{
    public abstract class ObjectiveFunctionValueCalculatotBase : ObjectiveFunctionCalculator
    {
        public abstract float calculate(Chromosome chromosome, float[,] cargoQuantityMatrix, float[,] costsMatrix);
        public abstract float[] calculate(List<Chromosome> chromosomes, float[,] cargoQuantityMatrix, float[,] costsMatrix);
        public abstract long[] getTimerValues();
       
        protected float[] prepareCargoQuantityVector(float[,] cargoQuantityMatrix)
        {
            float[] result = new float[cargoQuantityMatrix.GetLength(0) * cargoQuantityMatrix.GetLength(1)];

            int k = 0;
            for (int i = 0; i < cargoQuantityMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < cargoQuantityMatrix.GetLength(1); j++)
                {
                    result[k++] = cargoQuantityMatrix[i, j];
                }
            }

            return result;
        }

        protected float[,] prepareCostsMatrices(List<Chromosome> chromosomes, float[,] costsMatrix)
        {
            int chromosomeSize = chromosomes[0].getSize();
            int numberOfChromosomes = chromosomes.Count;

            float[,] result = new float[chromosomeSize * chromosomeSize, numberOfChromosomes];
            for (int i = 0; i < numberOfChromosomes; i++)
            {
                int j = 0;
                for (int k = 0; k < chromosomeSize; k++)
                {
                    for (int m = 0; m < chromosomeSize; m++)
                    {
                        result[j++, i] = costsMatrix[(int)chromosomes[i].getGene(k) - 1, (int)chromosomes[i].getGene(m) - 1];
                    }
                }
            }
            return result;
        }
    }
}
